package com.econ.powercloud.ems.observer;

import com.econ.powercloud.common.base.facade.util.JacksonUtil;
import com.econ.powercloud.ems.common.enums.CacheTopicType;
import com.econ.powercloud.ems.common.enums.TimingControlStatusEnum;
import com.econ.powercloud.ems.common.interf.AbstractEconBusProcess;
import com.econ.powercloud.ems.common.pojo.CacheOperationVO;
import com.econ.powercloud.ems.common.pojo.EmsTimingDTO;
import com.econ.powercloud.ems.service.common.CronTaskCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author hcx
 * @since 2023/8/17 10:33
 **/
@Component
public class TimingMemorySyncProcessor extends AbstractEconBusProcess {
    public TimingMemorySyncProcessor() {
        setCacheTopicType(CacheTopicType.TIMING_SYNC);
    }

    @Autowired
    CronTaskCommonService cronTaskCommonService;

    @Override
    public void operation(CacheOperationVO cacheOperationVO) {
        List<EmsTimingDTO> data = JacksonUtil.convertToList(cacheOperationVO.getData(),EmsTimingDTO.class);
        data.forEach(d->{
            Integer id = d.getId();
            Integer status = d.getStatus();
            //同步定时任务的启停状态
            if (status.equals(TimingControlStatusEnum.EFFECTIVE.getCode())) {
                cronTaskCommonService.addTask(id);
            } else {
                cronTaskCommonService.cancel(id);
            }
        });
    }
}
